// pages/impression/topic/topic.js
const app = getApp();
const http = require("../../../utils/http.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    WindowHeight: app.globalData.WindowHeight,
    ScreenHeight: app.globalData.ScreenHeight,

    pageIndex: 1,
    pageSize: 10,
    topicTitle: '',

    totalCount: 0,
    totalPage: 0,

    topicList: [],

    floorstatus: false,
    animationData: {},

    triggered: false
  },

  getData(param) {
    http.get("/topic/select", param).then(res => {
      this.setData({
        totalCount: res.data.data.totalCount,
        totalPage: res.data.data.totalPage,
        topicList: res.data.data.resultList
      })
    })
  },

  onRefresh() {
    if (this._freshing) return
    this._freshing = true
    this.refresh()

    this._freshing = false
  },

  // 获取滚动条当前位置
  scrolltoupper: function(e) {
    if (e.detail.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },

  inputChange(e) {
    this.setData({
      pageIndex: 1,
      pageSize: 10,
    })
    this.setData({
      topicTitle: e.detail.value
    })
    let param = {
      pageIndex: this.data.pageIndex,
      pageSize: this.data.pageSize,
      topicTitle: this.data.topicTitle
    }
    this.getData(param);
  },
  clearTopicTitle() {
    this.setData({
      topicTitle: ''
    })
    let param = {
      pageIndex: 1,
      pageSize: 10,
      topicTitle: ''
    }
    this.getData(param);
  },

  toLowFun(e) {
    let self = this;
    if (self.data.pageIndex >= self.data.totalPage) {
      return;
    }
    let param = {
      pageIndex: self.data.pageIndex + 1,
      pageSize: self.data.pageSize,
      topicTitle: self.data.topicTitle
    }
    self.setData({
      pageIndex: self.data.pageIndex + 1,
    })
    http.get("/topic/select", param).then(res => {
      let list = self.data.topicList
      self.setData({
        topicList: list.concat(res.data.data.resultList),
        totalCount: res.data.data.totalCount,
        totalPage: res.data.data.totalPage
      })
    })
  },

  //回到顶部
  goTop: function(e) { // 一键回到顶部
    this.setData({
      topNum: this.data.topNum = 0
    });
  },
  //刷新
  refresh: function(e) {
    let self = this;
    self.setData({
      topNum: self.data.topNum = 0
    });
    let id = setInterval(() => {
      app.rotateAni(self, 'animationData', 'num')
    }, 500)

    this.onReady();
    this.setData({
      triggered: false,
    })
    clearInterval(id);
  },

  createTopic() {
    let pages = getCurrentPages(); //页面栈
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({ //直接给上一个页面赋值
      topicId: '',
      topicTitle: '#' + this.data.topicTitle + '#' //自己定义的一个名字
    });
    wx.navigateBack({
      delta: 1
    })
  },

  addTopic(e) {
    let pages = getCurrentPages(); //页面栈
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({ //直接给上一个页面赋值
      topicId: e.currentTarget.dataset.id,
      topicTitle: e.currentTarget.dataset.topictitle //自己定义的一个名字
    });
    wx.navigateBack({
      delta: 1
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.setData({
      pageIndex: 1,
      pageSize: 10,
      topicTitle: ''
    })
    let param = {
      pageIndex: this.data.pageIndex,
      pageSize: this.data.pageSize,
      topicTitle: this.data.topicTitle
    }
    this.getData(param);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})